_Put_Sprite_Overwrite:
; A = x coordinate
; L = y coordinate
; B = number of rows
; 
; IX = address of sprite

	push af

	;We add clipping on this routine only for placing buildings
	;down.  Most of the sprite clipping comes from
	;masked sprites, not overwritten sprites.

	BIT Object_Ready_To_Add, (IY + flag) 
	jr z, ++_

	cp 80
	jr nz, _
	
	ld a, (Sprite_Width)
	cp 4
	jr nz, _

	SET Clip_Sprite, (IY + Flag)

_
	ld a, b
	cp 32
	jr nz, _
	ld a, l
	cp 48
	jr nz, _
	ld a, b
	sub 16
	ld b, a

_

	pop af

    LD     H, 0
    LD     D, H
    LD     E, L
    ADD    HL, HL
    ADD    HL, DE
    ADD    HL, HL
    ADD    HL, HL

    LD     E, A
    SRL    E
    SRL    E
    SRL    E
    ADD    HL, DE

    LD     DE, plotsscreen
    ADD    HL, DE
   
    
    AND    7
    JR     Z, __AlignedOverwrite

    LD     C, A
    PUSH HL
    LD A, 12
    LD HL, Sprite_Width
    SUB (HL)
    LD D,0
    LD E, A
    POP HL    
    
__RowLoopOverwrite:

    PUSH   BC
    
    ;Stores width into B, which will be decreased 1 by 1 until one row is complete
    LD A, (Sprite_Width)
    LD B, A
    

__ColumnLoopOverwrite:
    PUSH BC 
       
    LD     B, C
    LD     C, (IX)
    XOR    A

__ShiftLoopOverwrite:
    SRL    C
    RRA
  
    DJNZ   __ShiftLoopOverwrite
  

    INC    HL

    LD     (HL), A

    DEC    HL
    LD     A, C

    BIT Font_Mode, (IY + flag + 1)
    JR Z, _

    XOR (HL)

_

    LD     (HL), A

    POP BC
    INC HL
    INC IX
	BIT Clip_Sprite, (IY + flag)
	jr z, _

	dec b
_  
    DJNZ __ColumnLoopOverwrite

	BIT Clip_Sprite, (IY + flag)
	jr z, _
   
	INC HL
	INC HL
	INC IX
	INC IX
_ 
   ADD    HL, DE
   
    POP    BC
    DJNZ   __RowLoopOverwrite
    
    RES Clip_Sprite, (IY + flag)
    RET

__AlignedOverwrite:
   
    PUSH HL
    LD A, 12
    LD HL, Sprite_Width
    SUB (HL)
    LD D,0
    LD E, A
    POP HL  
     
__PutLoopOverwrite:
    PUSH BC
    LD A, (Sprite_Width)
    LD B, A 

__PutLoopOverwrite2:
    
    LD     A, (IX)


    
    LD     (HL), A
    INC  IX
  
    INC HL
	BIT Clip_Sprite, (IY + flag)
	jr z, _


	dec b
_

    DJNZ __PutLoopOverwrite2
__PutLoopOverwriteAdd:
    POP BC
	BIT Clip_Sprite, (IY + flag)
	jr z, _	
	INC HL
	INC HL
	INC IX
	INC IX
_    
    ;ADD    HL, DE
    ;DJNZ _PutLoop 
 ADD HL, DE
 DJNZ __PutLoopOverwrite
    RES Clip_Sprite, (IY + flag)
 RET



_Put_Sprite_XOR:
; A = x coordinate
; L = y coordinate
; B = number of rows
; 
; IX = address of sprite
    LD     H, 0
    LD     D, H
    LD     E, L
    ADD    HL, HL
    ADD    HL, DE
    ADD    HL, HL
    ADD    HL, HL

    LD     E, A
    SRL    E
    SRL    E
    SRL    E
    ADD    HL, DE

    LD     DE, plotsscreen
    ADD    HL, DE
   
    
    AND    7
    JR     Z, __AlignedXOR

    LD     C, A
    PUSH HL
    LD A, 12
    LD HL, Sprite_Width
    SUB (HL)
    LD D,0
    LD E, A
    POP HL    
    
__RowLoopXOR:

    PUSH   BC
    
    ;Stores width into B, which will be decreased 1 by 1 until one row is complete
    LD A, (Sprite_Width)
    LD B, A
    

__ColumnLoopXOR:
    PUSH BC 
       
    LD     B, C
    LD     C, (IX)
    XOR    A

__ShiftLoopXOR:
    SRL    C
    RRA
  
    DJNZ   __ShiftLoopXOR
  

    INC    HL
    XOR   (HL)
    LD     (HL), A

    DEC    HL
    LD     A, C
    XOR    (HL)
    LD     (HL), A

    POP BC
    INC HL
    INC IX
  
    DJNZ __ColumnLoopXOR
   
    ADD    HL, DE
   
    POP    BC
    DJNZ   __RowLoopXOR
    
    RET

__AlignedXOR:
   
    PUSH HL
    LD A, 12
    LD HL, Sprite_Width
    SUB (HL)
    LD D,0
    LD E, A
    POP HL  
     
__PutLoopXOR:
    PUSH BC
    LD A, (Sprite_Width)
    LD B, A 

__PutLoopXOR2:
    
    LD     A, (IX)
    XOR (HL)
    LD     (HL), A
    INC  IX
  
    INC HL
    DJNZ __PutLoopXOR2
__PutLoopXORAdd:
    POP BC
    
    ;ADD    HL, DE
    ;DJNZ _PutLoop 
 ADD HL, DE
 DJNZ __PutLoopXOR
    RET
